package com.hyt.it.ogt.kq.service.bm.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.kq.common.utils.KqStringUtils;
import com.hyt.it.ogt.kq.service.bm.mapper.InvoiceSettingMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.InvoiceEnterprise;
import com.hyt.it.ogt.kq.service.bm.model.entity.InvoiceSetting;
import com.hyt.it.ogt.kq.service.bm.model.vo.InvoiceSettingVo;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceEnterpriseService;
import com.hyt.it.ogt.kq.service.bm.service.IInvoiceSettingService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liteng
 * @since 2020-09-01
 */
@Service
public class InvoiceSettingServiceImpl extends BaseServiceImpl<InvoiceSettingMapper, InvoiceSetting> implements IInvoiceSettingService {

    @Autowired
    IInvoiceEnterpriseService iInvoiceEnterpriseService;

    @Transactional(rollbackFor = Exception.class)
    public void saveInvoiceSetting(InvoiceSettingVo vo) throws Exception {
        try{
            if(!KqStringUtils.isEmpty(vo.getInvoiceType())&&("1".equals(vo.getInvoiceType()) || "2".equals(vo.getInvoiceType()))){
                QueryWrapper<InvoiceSetting> wrapper = new QueryWrapper<InvoiceSetting>();
                wrapper.eq("office_id",vo.getOfficeId());
                wrapper.eq("invoice_type",vo.getInvoiceType());
                InvoiceSetting invoiceSetting = this.getOne(wrapper);
                if (invoiceSetting == null) {
                    InvoiceSetting tempSetting = new InvoiceSetting();
                    BeanUtils.copyProperties(vo,tempSetting);
                    tempSetting.setId(UUIDUtils.newSortUUID());
                    InvoiceEnterprise enterprise = new InvoiceEnterprise();
                    enterprise.setAddress(vo.getAddress());
                    enterprise.setContent(vo.getContent());
                    enterprise.setTelphone(vo.getTelphone());
                    enterprise.setInvoiceType(vo.getInvoiceType());
                    enterprise.setOfficeId(vo.getOfficeId());
                    enterprise.setBank(vo.getBank());
                    enterprise.setEnterpriseName(vo.getEnterpriseName());
                    enterprise.setInvoiceType(vo.getInvoiceType());
                    enterprise.setTaxNum(vo.getTaxNum());
                    enterprise.setTaxPercent(vo.getTaxPercent());
                    enterprise.setId(UUIDUtils.newSortUUID());
                    this.save(tempSetting);
                    iInvoiceEnterpriseService.save(enterprise);
                } else {
                    InvoiceSetting tempSetting = vo;
                    tempSetting.setId(invoiceSetting.getId());
                    QueryWrapper<InvoiceEnterprise> enterWrapper = new QueryWrapper<InvoiceEnterprise>();
                    enterWrapper.eq("office_id",vo.getOfficeId());
                    enterWrapper.eq("invoice_type",vo.getInvoiceType());
                    InvoiceEnterprise tempInvoiceEnterprise = iInvoiceEnterpriseService.getOne(enterWrapper);
                    InvoiceEnterprise enterprise = new InvoiceEnterprise();
                    enterprise.setAddress(vo.getAddress());
                    enterprise.setContent(vo.getContent());
                    enterprise.setTelphone(vo.getTelphone());
                    enterprise.setInvoiceType(vo.getInvoiceType());
                    enterprise.setOfficeId(vo.getOfficeId());
                    enterprise.setBank(vo.getBank());
                    enterprise.setEnterpriseName(vo.getEnterpriseName());
                    enterprise.setInvoiceType(vo.getInvoiceType());
                    enterprise.setTaxNum(vo.getTaxNum());
                    enterprise.setTaxPercent(vo.getTaxPercent());
                    enterprise.setId(tempInvoiceEnterprise.getId());
                    this.saveOrUpdate(tempSetting);
                    iInvoiceEnterpriseService.saveOrUpdate(enterprise);
                }
            } else {
                throw new Exception("14076");
            }
        } catch (Exception ex){
            throw ex;
        }
    }

    public InvoiceSettingVo queryInvoice(String officeId,String invoiceType){
        QueryWrapper<InvoiceSetting> queryWrapper = new QueryWrapper<InvoiceSetting>();
        queryWrapper.eq("office_id",officeId);
        queryWrapper.eq("invoice_type",invoiceType);
        InvoiceSetting invoiceSetting = this.getOne(queryWrapper);
        QueryWrapper<InvoiceEnterprise> enterpriseWrapper = new QueryWrapper<InvoiceEnterprise>();
        enterpriseWrapper.eq("office_id",officeId);
        enterpriseWrapper.eq("invoice_type",invoiceType);
        InvoiceEnterprise enterprise = iInvoiceEnterpriseService.getOne(enterpriseWrapper);
        InvoiceSettingVo vo = new InvoiceSettingVo();
        if (enterprise != null) {
            vo.setAddress(enterprise.getAddress());
            vo.setContent(enterprise.getContent());
            vo.setInvoiceType(enterprise.getInvoiceType());
            vo.setBank(enterprise.getBank());
            vo.setTelphone(enterprise.getTelphone());
            vo.setEnterpriseName(enterprise.getEnterpriseName());
            vo.setInvoiceType(enterprise.getInvoiceType());
            vo.setTaxNum(enterprise.getTaxNum());
            vo.setTaxPercent(enterprise.getTaxPercent());
            vo.setEnterpriseId(enterprise.getId());
        }
        if (invoiceSetting != null) {
            vo.setId(invoiceSetting.getId());
            vo.setInvoiceType(invoiceSetting.getInvoiceType());
            vo.setAddrCity(invoiceSetting.getAddrCity());
            vo.setAddrDetail(invoiceSetting.getAddrDetail());
            vo.setAddressee(invoiceSetting.getAddressee());
            vo.setExpressFee(invoiceSetting.getExpressFee());
            vo.setAddrCounty(invoiceSetting.getAddrCounty());
            vo.setAddrProv(invoiceSetting.getAddrProv());
            vo.setPhone(invoiceSetting.getPhone());
            vo.setReceivingType(invoiceSetting.getReceivingType());
        }
        return vo;
    }
}
